<template>
  <div class="goods-list-container">
    <my-header title="商品列表"></my-header>
    <my-table :list="list">
      <template #header>
        <th>编号</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="scpoe">
        <td>{{ scpoe.row.id }}</td>
        <td>{{ scpoe.row.goods_name }}</td>
        <td>{{ scpoe.row.goods_price }}</td>
        <td>
          <input
            v-model="scpoe.row.inputValue"
            class="tag-input from-control"
            style="width: 100px"
            v-if="scpoe.row.inputVisible"
            @blur="scpoe.row.inputVisible = false"
            @keydown.enter="enterFn(scpoe.row)"
            @keydown.esc="scpoe.row.inputValue = ''"
            type="text"
          />
          <button
            style="display: block"
            class="btn btn-primary btn-sm add-tag"
            @click="scpoe.row.inputVisible = true"
          >
            +Tag
          </button>
          <span
            style="margin-left: 5px"
            class="bage bg-warning"
            v-for="(item, index) in scpoe.row.tags"
            :key="index"
            >{{ item }}</span
          >
        </td>
        <td>
          <button @click="del(scpoe.row.id)" class="btn btn-danger">
            删除
          </button>
        </td>
      </template>
    </my-table>
  </div>
</template>

<script>
import MyHeader from "../components/MyHeader.vue";
import MyTable from "../components/MyTable.vue";

export default {
  components: { MyHeader, MyTable },
  name: "GoodsList3Mygoodslist",

  data() {
    return {
      list: [],
    };
  },

  async created() {
    let res = await this.$http({
      url: "/api/goods",
    });
    this.list = res.data.data;
  },

  mounted() {},

  methods: {
    del(id) {
      let index = this.list.findIndex((item) => item.id == id);
      this.list.splice(index, 1);
    },
    enterFn(obj) {
      // 回车
      // console.log(obj.inputValue);

      if (obj.inputValue.trim().length === 0) return alert("请输入数据");
      this.$emit("enterFn", obj.row);

      obj.tags.push(obj.inputValue); // 表单里的字符串状态tags数组
      obj.inputValue = "";
    },
  },
};
</script>

<style lang="less" scoped>
.goods-list-container {
  padding: 45px 0 45px 0;
}
</style>